<?php
/**
 * Mechanism
 *
 * LICENSE
 *
 * Copyright (c) 2011, Sergey A. Ivonchik <BreatheInMyVoid@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above
 *   copyright notice, this list of conditions and the following disclaimer
 *   in the documentation and/or other materials provided with the
 *   distribution.
 * * Neither the name of the  nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @package Mechanism
 * @license http://www.opensource.org/licenses/bsd-license
 * @author Sergey A. Ivonchik <BreatheInMyVoid@gmail.com>
 * @copyright Copyright (c) 2011 Sergey A. Ivonchik
 */

namespace Mechanism\Router {
	/**
	 * Test for Basic router
	 *
 	 * @package Mechanism\Router
 	 * @author BreatheInMyVoid <BreatheInMyVoid@gmail.com>
	 */
	class BasicTest extends \PHPUnit_Framework_TestCase {
		/**
		 * Router for tests
		 *
		 * @var Basic $router
		 */
		protected $router = null;

		/**
		 * Route for tests
		 *
		 * @var Route\Uri
		 */
		protected $route = null;

		/**
		 * Setup method
		 */
		public function setUp( ) {
			/* Default route */
			$this->route = new Route\Uri( '[controller]{s}[action]' );
			$this->route->setDefault( 'controller', 'index' )
						->setDefault( 'action', 'index' );
			
			/* Default router */
			$this->router = new Basic( $this->route );
		}

		/**
		 * Test for constructors
		 * 
		 * @test
		 */
		public function constructor( ) {
			/* Test for empty constructor */
			$router = null;
			
			$router = new Basic( );
			
			$this->assertNotNull( $router );
		}
		
		/**
		 * Test for setRoute( $route )
		 *
		 * @test
		 * @return Basic $router
		 */
		public function setRoute( ) {
			/* Test setRoute( $route ) method and fluent interface of it */
			$this->assertSame(
				$this->router,
				$this->router->setRoute( $this->route )
			);			
			
			return $this->router;
		}

		/**
		 * Test for getRoute( )
		 *
		 * @test
		 * @depends setRoute
		 * @param Basic $router
		 */
		public function getRoute( $router ) {
			/* Test getRoute( ) method */	
			$this->assertSame(
				$this->route,
				$this->router->getRoute( )
			);		
		}

		/**
		 * Test for routing( $path )
		 * 
		 * @test
		 */
		public function routing( ) {
			/* Try if route is null */
			try {
				$badRoute = new Basic( );
				$badRoute->routing( 'http://www.example.com/' );
			} catch ( Exception\Runtime $e ) {
				$testRouteA =  'http://www.example.com/';
				$testResultA = array( 'controller' => 'index',
									  'action' => 'index' );
				
				$testRouteB =  'http://www.example.com/news/show/';
				$testResultB = array( 'controller' => 'news',
									  'action' => 'show' );
				
				$testRouteC =  'http://www.example.com/some/wrong/way';
				$testResultC = null;
				
				/* Test A */
				$this->assertTrue( 
					$this->router->routing( $testRouteA )
				);
				
				$this->assertEquals(
					$testResultA,
					$this->router->getMatchedRoute( )->get( )
				);
				
				/* Test B */
				$this->assertTrue( 
					$this->router->routing( $testRouteB )
				);
				
				$this->assertEquals(
					$testResultB,
					$this->router->getMatchedRoute( )->get( )
				);
							
				/* Test C */
				$this->assertFalse( 
					$this->router->routing( $testRouteC )
				);
				
				return;
			}
			
			$this->fail('An expected Runtime exception has not been raised.');
		}

		/**
		 * Test for getMatchedRoute( )
		 * 
		 * In Basic router getRoute and getMatchedRoute
		 * always equals, cause we have only one route.
		 * BUT...getMatchedRoute returns null if route 
		 * does not match path.
		 * 
		 * @test
		 */
		public function getMatchedRoute( ) {
			 /* Cause last test falls (some/wrong/way) getMatchedRoute returns null */	
			$this->assertNull(
				$this->router->getMatchedRoute( )
			);
			
			/* New routing */
			$this->assertTrue( 
				$this->router->routing( 'http://www.example.com/news' )
			);			
			
			/* Test getMatchedRoute */
			$this->assertSame(
				$this->route,
				$this->router->getMatchedRoute( )
			);
			
			/* Test result */
			$this->assertEquals(
				array( 'controller' => 'news',
					   'action' => 'index' ),
				$this->router->getMatchedRoute( )->get( )
			);			
		}
	}
}